import { history } from 'umi';

let extraRoutes: { path: any; }[] | { path: string; component: string; }[];
import user1 from '@/pages/user1';

export function patchRoutes({ routes }: any) {
  // console.log(routes, '前');
  routes.unshift({
    path: '/foo',
    component: user1,
  });
  extraRoutes.map(item => {
    console.log(item);
    routes.unshift({
      path: item.path,
      // @ts-ignore
      component: require(`@/pages${item.component}`).default,
    });
  });
  // console.log(routes, '后');

}

export function render(oldRender: () => void) {
  // 模拟服务端请求获得的路由
  extraRoutes = [
    {
      path: '/server',
      component: '/user1',
    },
  ];

  // fetch('/api/routes').then(res=>res.json()).then((res) => {
  // extraRoutes = res.routes;
  // 渲染前左权限校验
  const isLogin = false;
  if (!isLogin) {
    history.push('/login')
  }
  oldRender();
  // })
}
